package ru.urfc.portal.developer_tasks.ejb_tests;

import java.util.Date;

import javax.ejb.EJB;

import ru.urfc.portal.dao.RadioDao;
import ru.urfc.portal.dao.SpectrId;
import ru.urfc.portal.developer_tasks.BaseTestEjb;
import ru.urfc.portal.model.Client;
import ru.urfc.portal.model.Branch;
import ru.urfc.portal.model.NotificationSettings;
import ru.urfc.portal.model.accounting.Contragent;
import ru.urfc.portal.model.radio.DeviceWithExtra;
import ru.urfc.portal.model.radio.NetLicense;
import ru.urfc.portal.model.radio.NetWithLicense;

public class RadioDaoTestEjb extends BaseTestEjb {

	@EJB RadioDao radioDao;
	
	NetWithLicense net;
	SpectrId netId;

	@Override
	protected void setUp() throws Exception {
		Contragent contragent = persist(new Contragent());
		net = persist(new NetWithLicense(contragent));
		netId = new SpectrId(net.getId(), Branch.SVERD);		
	}
	
	public void testGetNetCurrentLicense() {
		Date date = new Date();
		NetLicense license = persist(new NetLicense(net, "", date, date, null));
		assertEquals(license.getId(), radioDao.getNetCurrentLicense(netId, new Date()).getId());
	}

	public void testGetNetCurrentLicenseTwoWithSameDate() {
		Date date = new Date();
		persist(new NetLicense(net, "", date, date, null));
		persist(new NetLicense(net, "", date, date, null));
		radioDao.getNetCurrentLicense(netId, new Date()).getId();
	}

	public void testGetNets() throws Exception {
		Branch branch = exactlyOne(find(Branch.class, "code", Branch.SVERD.getCode()));
		Contragent contragent = persist(new Contragent());
		Client client = persist(new Client(contragent, branch, new NotificationSettings()));
		
		NetWithLicense net = persist(new NetWithLicense(contragent));
		assertEquals(1, radioDao.getNets(client, new Date()).size());
		assertEquals(0, radioDao.getNets(client, new Date()).get(0).getnDevices());
		persist(new DeviceWithExtra(net));
		assertEquals(1, radioDao.getNets(client, new Date()).size());
		assertEquals(1, radioDao.getNets(client, new Date()).get(0).getnDevices());
		persist(new DeviceWithExtra(net));
		assertEquals(1, radioDao.getNets(client, new Date()).size());
		assertEquals(2, radioDao.getNets(client, new Date()).get(0).getnDevices());
	}
	
}
